home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #14 / Monster Media No. 14 (April 1996) (Monster Media, Inc.).ISO / prog_bas / netuser.zip / DATA / VB / NetUser / 16-Bit / NetUser.bas < prev   
BASIC Source File  |  1995-12-18  |  6KB  |  163 lines

  1. Option Explicit
  2. '
  3. '   This module will return the user name of the person who signed into
  4. '   the system. This module should work with the following operating
  5. '   systems: Windows 3.x, Windows for Workgroups, Windows 95 and
  6. '   Windows NT.
  7. '
  8. '   This module is written for 16 bit languages. If you are using a 32-bit program
  9. '   then you should use the proper version of this file.
  10. '
  11. '   If the user will be running on Windows 95 or Windows NT then this module
  12. '   requires the CALL32.DLL file to function correctly. This DLL should be
  13. '   included with your application and copied to the users SYSTEM directory
  14. '   under windows.
  15. '
  16. ''''
  17. '
  18. '   Declare variables needed
  19. '
  20. Dim glngReturnStatus As Long
  21. Dim mintInitialized As Integer
  22. Dim mlngGetUserName As Long
  23.  
  24. Const SUCCESS = 1&
  25. Const FAILURE = 0&
  26. Const WV_WIN3X = 0
  27. Const WV_WINWFW = 1
  28. Const WV_WINNT = 2
  29. Const WV_WIN95 = 3
  30. '
  31. '   API Declaration
  32. '
  33. Declare Function KRN_GetVersion Lib "Kernel" Alias "GetVersion" () As Integer
  34. Declare Function KRN_GetWinFlags Lib "Kernel" Alias "GetWinFlags" () As Integer
  35. Declare Function USR_WNetGetCaps Lib "User" Alias "WNetGetCaps" (ByVal nIndex As Integer) As Integer
  36. Declare Function WFW_MNetNetworkEnum Lib "WFWNET.DRV" Alias "MNetNetworkEnum" (nIndex As Integer) As Integer
  37. Declare Function WFW_MNetSetNextTarget Lib "WFWNET.DRV" Alias "MNetSetNextTarget" (ByVal nIndex As Integer) As Integer
  38. Declare Function USR_WNetGetUser Lib "User" Alias "WNetGetUser" (ByVal sUser As String, nBufferSize As Integer) As Integer
  39. Declare Function Declare32& Lib "call32.dll" (ByVal func$, ByVal library$, ByVal args$)
  40. Declare Function GetUserNameA Lib "call32.dll" Alias "call32" (ByVal strUser As String, lngUserBuffer As Long, ByVal lngID As Long) As Integer
  41.  
  42.  
  43. Function NetworkUserID() As String
  44. '   This routine will get the name of the user signed onto the network.
  45. '   If no username is found it will return an UnknownUser string.
  46. '
  47.     Dim lngBufferSize As Long
  48.     Dim strUser As String
  49.     
  50.     On Error GoTo NetworkUserID_EH
  51.  
  52.     NetworkUserID = "UnknownUser"
  53.     
  54.     lngBufferSize = 255
  55.     strUser = Space$(lngBufferSize)
  56. '
  57. '   Declare some variable/constants needed for 16-bit
  58. '
  59.     Dim intHandle As Integer
  60.     Dim intEnumerate As Integer
  61.     Dim intVersion As Integer
  62. '
  63. '   Get the users current windows version
  64. '
  65.     intVersion = WindowsVersion()
  66.     Select Case intVersion
  67.     Case WV_WIN3X
  68.         glngReturnStatus = USR_WNetGetUser(strUser, CInt(lngBufferSize))
  69.         If (glngReturnStatus = 0) Then
  70.             strUser = Left$(strUser, InStr(strUser, Chr(0)) - 1)
  71.         End If
  72.     Case WV_WINWFW
  73.         intHandle = 0
  74.         intEnumerate = 0
  75.         intEnumerate = WFW_MNetNetworkEnum(intHandle)
  76. '
  77. '   Scan through the networks until we get a name
  78. '
  79.         While (intEnumerate = 0)
  80.             glngReturnStatus = WFW_MNetSetNextTarget(intHandle)
  81.             glngReturnStatus = USR_WNetGetUser(strUser, CInt(lngBufferSize))
  82.             If (glngReturnStatus = 0) Then
  83.                 strUser = Left$(strUser, InStr(strUser, Chr(0)) - 1)
  84.             End If
  85.             intEnumerate = WFW_MNetNetworkEnum(intHandle)
  86.         Wend
  87.     Case WV_WINNT, WV_WIN95
  88. '
  89. '   Initialize and call the Win32 API function(s)
  90. '
  91.         mlngGetUserName = Declare32("GetUserNameA", "advapi32.dll", "pp")
  92.         glngReturnStatus = GetUserNameA(strUser, lngBufferSize, mlngGetUserName)
  93.         If glngReturnStatus <> SUCCESS Then
  94.             MsgBox "Problem during UserName, problem code is " & Error
  95.             strUser = "UnknownUser"
  96.             Exit Function
  97.         End If
  98.         strUser = Left$(strUser, lngBufferSize - 1)
  99.     End Select
  100.     NetworkUserID = strUser
  101.     Exit Function
  102.  
  103. NetworkUserID_EH:
  104.     NetworkUserID = "UnknownUser"
  105.     Exit Function
  106. End Function
  107.  
  108. Private Function WindowsVersion() As Integer
  109. '
  110. '   This routine will determine the DOS/Windows version(s).
  111. '   It will return the values back to the calling program.
  112. '
  113.     Dim strLowByte As String
  114.     Dim strHighByte As String
  115.     Dim sglWindowsVersion As Single
  116.     Dim intNetwork As Integer
  117.     
  118.     Const WNNC_NET_MultiNet = &H8000
  119.     Const WNNC_SUBNET_WinWorkgroups = 4
  120.     Const WNNC_NET_TYPE = 2
  121.     Const WF_WINNT = &H4000
  122.  
  123.     On Error GoTo WindowsVersion_EH
  124.     
  125.     glngReturnStatus = KRN_GetWinFlags()
  126.     If glngReturnStatus And WF_WINNT Then
  127.         WindowsVersion = WV_WINNT
  128.     Else
  129. '
  130. '   Since Windows NT is not running, find the version of windows
  131. '
  132.         glngReturnStatus = KRN_GetVersion()
  133.         glngReturnStatus = glngReturnStatus And &HFFFF&
  134.         strLowByte = Trim$(CStr(glngReturnStatus And &HFF))
  135.         strHighByte = Trim$(CStr((glngReturnStatus And &HFF00) / 256))
  136.         sglWindowsVersion = CSng(strLowByte & "." & strHighByte)
  137.         
  138.         Select Case sglWindowsVersion
  139.         Case Is < 3.95                 ' User is not under Windows 95
  140. '
  141. '   Check to see if the user is running WFW 3.11
  142. '
  143.             intNetwork = USR_WNetGetCaps(WNNC_NET_TYPE)
  144.             If (intNetwork And WNNC_NET_MultiNet) Then
  145.                 If ((intNetwork And &HFFFF) And WNNC_SUBNET_WinWorkgroups) <> 0 Then
  146.                     WindowsVersion = WV_WINWFW
  147.                 Else
  148.                     WindowsVersion = WV_WIN3X
  149.                End If
  150.             Else
  151.                 WindowsVersion = WV_WIN3X
  152.             End If
  153.         Case Else
  154.             WindowsVersion = WV_WIN95
  155.         End Select
  156.     End If
  157.     Exit Function
  158.  
  159. WindowsVersion_EH:
  160.     MsgBox "Problem in WindowsVersion, problem is " & Error
  161.     Exit Function
  162. End Function
  163.